package com.example.demo.config;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ling
 * Date: 2023-01-12
 * Time: 17:32
 * 同一异常处理
 */
@ControllerAdvice
@ResponseBody
public class ErrorAdvice {
    @ExceptionHandler(Exception.class)

    public HashMap<String,Object> exAdvice(Exception e) {
        HashMap<String,Object> result = new HashMap<>();
        result.put("code",-1);
        result.put("msg",e.getMessage());
        return result;
    }

    @ExceptionHandler(ArithmeticException.class) //捕捉算术异常
    public HashMap<String,Object> arithmeticAdvice(ArithmeticException e) {
        HashMap<String,Object> result = new HashMap<>();
        result.put("code",-2);
        result.put("msg",e.getMessage());
        return result;
    }
}
